/*
 * Copyright (C) 2011 TargetBoard
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jaxb;


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jaxb.generated.book.Book;
import jaxb.generated.book.History;
import jaxb.generated.book.Record;
import jaxb.generated.datatable.Column;
import jaxb.generated.datatable.Row;
import jaxb.generated.datatable.Table;
import jaxb.generated.users.User;
import jaxb.generated.users.Users;
import jaxb.wrappers.ColumnWrapper;
import jaxb.wrappers.HistoryWrapper;
import jaxb.wrappers.RecordWrapper;
import jaxb.wrappers.RowWrapper;

import contrl.model.Model;

/**
 * @author amc
 *
 */
public class XmlModel implements Model{
	private XmlManager xmlManager;

	public XmlModel() {
		xmlManager = XmlManager.getInstance();
	}

	private Record findBookRecord(String key) throws InitailizedException {
		Record record = null;
		Book book = xmlManager.getBook();
		for (Record rec : book.getRecord()) {
			if (rec.getKey().equals(key)) {
				record = rec;
				break;
			}
		}
		return record;
	}
	public Map<String, List<contrl.model.data.book.History>> generateHistory() throws InitailizedException {
		Map<String, List<contrl.model.data.book.History>> history = new HashMap<String, List<contrl.model.data.book.History>>();
		Book book = xmlManager.getBook();
		for (Record rec : book.getRecord()) {
			List<contrl.model.data.book.History> historyList = new ArrayList<contrl.model.data.book.History>();
			for(History hist:rec.getHistory()){
				historyList.add(new HistoryWrapper(hist));
			}
			Collections.sort(historyList);
			history.put(rec.getKey(),historyList);
		}
		return history;
	}

	public List<contrl.model.data.table.Row> getModelTable() throws InitailizedException {
		List<contrl.model.data.table.Row> bookList = new ArrayList<contrl.model.data.table.Row>();
		Table table = xmlManager.getTable();
		for (Row row : table.getRow()) {
			String keyValue = null;
			RowWrapper rowWrapper = new RowWrapper(row);
			for (Column column : row.getColumn()) {
				if (column.isRequired() == null || column.isRequired() == false) {
					continue;
				} else if (column.isKey() != null && column.isKey() == true) {
					keyValue = column.getContent();
					rowWrapper.setKey(keyValue);
				}
				
				rowWrapper.getColumn().add(new ColumnWrapper(column));

			}
			addBookColumns(rowWrapper, keyValue);
			bookList.add(rowWrapper);

			// addHistory(bookList, keyValue);
		}
		return bookList;
	}

	public Map<String, List<String>> getDescription() throws InitailizedException {
		Map<String, List<String>> desc = new HashMap<String, List<String>>();
		Table table = xmlManager.getTable();
		for (Row row : table.getRow()) {

			String key = "";
			List<String> descRow = new ArrayList<String>();
			for (Column column : row.getColumn()) {
				if (column.isRequired() == null || column.isRequired() == false) {
					descRow.add(column.getContent());
				}
				if (column.isKey() != null && column.isKey() == true) {
					key = column.getContent();
				}
			}
			if (row.isHeader() != null && row.isHeader()) {
				desc.put("header", descRow);
			} else {
				desc.put(key, descRow);
			}
		}
		return desc;
	}

//	private void addHistory(List<RowWrapper> bookList, String key) throws InitailizedException {
//		int rowLength = bookList.get(0).getColumn().size() - 2;
//		Record record = findBookRecord(key);
//		if (record != null) {
//			for (HistoryWrapper history : record.getHistory()) {
//				HistoryWrapper historyWrapper = new HistoryWrapper(history);
//				RowWrapper rowWrapper = new RowWrapper(false, true);
//				for (int i = 0; i < rowLength; i++) {
//					rowWrapper.getColumn().add(new ColumnWrapper(""));
//				}
//				rowWrapper.getColumn().add(new ColumnWrapper(historyWrapper.getDate()));
//				rowWrapper.getColumn().add(new ColumnWrapper(historyWrapper.getUser()));
//				bookList.add(rowWrapper);
//			}
//		}
//	}

	private void addBookColumns(RowWrapper rowWrapper, String keyValue) throws InitailizedException {
		Record record = findBookRecord(keyValue);
		if (record != null) {
			RecordWrapper recordWrapper = new RecordWrapper(record);
			rowWrapper.getColumn().add(new ColumnWrapper(recordWrapper.getDate(),false,true));
			rowWrapper.getColumn().add(new ColumnWrapper(recordWrapper.getUser(),false,true));
		} else {
			if (rowWrapper.isHeader()) {
				rowWrapper.getColumn().add(new ColumnWrapper("Reservation date",false, true));
				rowWrapper.getColumn().add(new ColumnWrapper("Reserved by",false, true));
			} else {
				rowWrapper.getColumn().add(new ColumnWrapper("",false, true));
				rowWrapper.getColumn().add(new ColumnWrapper("",false, true));
			}
		}
	}

	public List<String> getUsersList() throws InitailizedException {
		List<String> usersList = new ArrayList<String>();
		Users users = xmlManager.getUsers();
		for (User user : users.getUser()) {
			usersList.add(user.getLastName() + " " + user.getFirsName());
		}
		Collections.sort(usersList);
		return usersList;
	}
}
